package com.cqc.byeburgerdemo01;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private NoScrollViewPager noScrollViewPager;
    private BottomNavigationView bottomView;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        initViews();
        //api>=15
    }

    private void initViews() {
        initToolbar();
        initViewPager();
        initBottomNavigationView();
    }

    private void initBottomNavigationView() {
        bottomView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.tab1:
                        noScrollViewPager.setCurrentItem(0);
                        break;
                    case R.id.tab2:
                        noScrollViewPager.setCurrentItem(1);
                        break;
                    case R.id.tab3:
                        noScrollViewPager.setCurrentItem(2);
                        break;
                    case R.id.tab4:
                        noScrollViewPager.setCurrentItem(3);
                        break;
                }
                return true;
            }
        });
    }

    private void initViewPager() {
        initData();
        noScrollViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragList.get(position);
            }

            @Override
            public int getCount() {
                return fragList.size();
            }
        });
    }

    private List<Fragment> fragList = new ArrayList<>();

    private void initData() {
        fragList.add(HomeFragment.getInstance());
        fragList.add(HolderFragment.getInstance("tab2"));
        fragList.add(HolderFragment.getInstance("tab3"));
        fragList.add(HolderFragment.getInstance("tab4"));
    }

    private void initToolbar() {
        toolbar.setTitle("Title");
        setSupportActionBar(toolbar);
    }

    private void findViews() {
        noScrollViewPager = (NoScrollViewPager) findViewById(R.id.noScrollViewPager);
        bottomView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
    }
}
